package com.doit.day02

import scala.io.Source

/**
 * 需求：
 * 1.求每部电影的平均分，按照降序输出
 * 2.求每个用户评过电影的总数，按照降序排列
 * 3.每个用户评分的前三部电影(用户，电影信息，评分值)
 * 4.哪些年份的电影评分(平均分)最高，取最高的前五个年份
 */
object MovieDemo1 {
  def main(args: Array[String]): Unit = {
    /*
    2.求每个用户评过电影的总数，按照降序排列

     */

    //读文件
//    val movie: List[String] = Source.fromFile("C:\\Users\\yi\\Desktop\\scala\\movies.txt").getLines().toList
    val rating: List[String] = Source.fromFile("C:\\Users\\yi\\Desktop\\scala\\ratings.txt").getLines().toList
//    val user: List[String] = Source.fromFile("C:\\Users\\yi\\Desktop\\scala\\users.txt").getLines().toList
    //2.将每一行的数据转换成 ==》对象
    //movie
  /*    val movie_info: List[Movie] = movie.map(line => {
        ////1_Toy Story (1995)_Animation|Children's|Comedy
        var movie_id: String = ""
        var movie_name: String =""
        var movie_type: String =""

        try{
          val arr: Array[String] = line.split("_")
          movie_id = arr(0)
          movie_name = arr(1)
          movie_type = arr(2)
        }catch{
          case e:Exception => println(e)
        }
        Movie(movie_id, movie_name, movie_type)
      })

   //转换将list集合中的movie对象转换成元组(map)  key movie_id，value 就是movie对象
   val movie_id_name: Map[String, Movie] = movie_info.map(movie => {
     (movie.movie_id, movie)
   }).toMap*/
      //user
/*      val user_info: List[Movie_User] = user.map(line => {
        var user_id: String = ""
        var gender: String = ""
        var age: Int = 0
        var job: String = ""
        var code: String = ""
        //1_F_16_10_48067
        //user_id:String,gender:String,age:Int,Job:String,code:String
        try {
          val arr: Array[String] = line.split("_")
          user_id = arr(0)
          gender = arr(1)
          age = arr(2).toInt
          job = arr(3)
          code = arr(4)
        } catch {
          case e: Exception => println(e)
        }
        Movie_User(user_id, gender, age, job, code)
      })*/

    //rating
    val rating_info: List[Rating] = rating.map(line => {
      var user_id: String = ""
      var movie_id: String = ""
      var rating: Double = 0
      var timeStamp: String = ""
      val arr: Array[String] = line.split("_")
      try {
        ////1_1193_5_978300760
        user_id = arr(0)
        movie_id = arr(1)
        rating = arr(2).toDouble
        timeStamp = arr(3)
      } catch {
        case e: Exception => println(e)
      }
      Rating(user_id, movie_id, rating, timeStamp)
    })


    //rating_info 对他进行一个map操作 ==》 （user_id,list(所有的电影id)）
    val user_movie: List[(String, String)] = rating_info.map(rat => {
      val user_id: String = rat.user_id
      val movie_id: String = rat.movie_id
      (user_id, movie_id)
    })

    //==>按照用户id进行分组
    val map: Map[String, List[(String, String)]] = user_movie.groupBy(_._1)

    val res: List[(String, Int)] = map.map(map => {
      //没有考虑一个用户多次评论同一部电影的情况
      (map._1, map._2.size)
      //考虑了一个用户多次评论同意不电影，针对这种情况进行了去重
      //      (map._1,map._2.distinct.size)
    }).toList.sortWith((tp1, tp2) => tp1._2 > tp2._2)

    //name  ==》求前10名

    for(i <- 0 to 9){
      println(res(i))
    }





  }
}
/*

//1_Toy Story (1995)_Animation|Children's|Comedy
case class Movie(movie_id: String, movie_name: String, movie_type: String)

//1_F_16_10_48067
case class Movie_User(user_id: String, gender: String, age: Int, job: String, code: String)

//1_1193_5_978300760
case class Rating(user_id: String, movie_id: String, rating: Double, timeStamp: String)*/
